from kivy.app import App
from kivy.clock import Clock
from kivy.logger import Logger
from kivy.graphics import Canvas, Color, Callback
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.carousel import Carousel
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.widget import Widget

from math import floor, ceil

__all__ = ('TargetSlide', )

class _TargetDisplay(Widget):

    def __init__(self, target_model, **kwargs):
        Widget.__init__(self, **kwargs)
        self.canvas = Canvas()
        self._target_model = target_model
        self.max_feet = 0
        self.bind(size=self.handle_on_size)
        
    @property
    def _target_view_size(self):
        return (self._target_model._view_width, self._target_model._view_height)

    def get_max_feet_for_view_size(self, view_size):
        return self._target_model.calc_max_indoor_distance_for_view_size(view_size)
            
    def set_indoor_distance_feet(self, feet):
        self._target_model.indoor_distance_feet = feet

    def handle_on_size(self, instance, value):
        tvs = self._target_view_size
        if (value[0] != tvs[0]) and (value[1] != tvs[1]):
            # targets
            self._target_model.position_graphics(view_size=value)
            self._undraw()
        
    def _undraw(self):
        self.canvas.clear()
        self.canvas.ask_update()
        Clock.schedule_once(self.draw, 0.1)
    
    def draw(self, instance):
        canvas = self.canvas
        graphics = self._target_model.get_graphics()
        canvas.add(Color(1.0, 1.0, 1.0, 1.0))
        for i in graphics:
            canvas.add(i)
        canvas.ask_update()


class TargetSlide(GridLayout):
    def __init__(self, target, **kwargs):
        GridLayout.__init__(self, **kwargs)
        self.cols = 2
        self.bind(size=self.handle_on_size)
        # target graphic
        self.target = target
        self._target_display = _TargetDisplay(target,
                                              size_hint=(1.0, 1.0))
        self.add_widget(self._target_display)
        # target description
        text = '\n'.join([self.target.name, self.target.description])
        self._label = Label(text=text)
        self.add_widget(self._label)
        #gridlayout = GridLayout(cols=1, spacing=0, size_hint=(None, None))
        #gridlayout.bind(minimum_height=gridlayout.setter('height'),
        #                minimum_width=gridlayout.setter('width'))
        #gridlayout.add_widget(self._label)
        #scrollview = ScrollView()
        #scrollview.add_widget(gridlayout)
        #self.add_widget(scrollview)

    def handle_on_size(self, instance, value):
        value_w = int(floor(value[0] / 2))
        text_w = value_w
        # targets
        self._target_display.size = (value_w, value[1])
        # label
        self._label.size = (text_w, value[1])
        self._label.text_size = (text_w, None)

    # setup by the view model
        
    def vm_get_max_feet_for_view_size(self, view_size):
        return self._target_display.get_max_feet_for_view_size(view_size)
            
    def vm_set_indoor_distance_feet(self, feet):
        return self._target_display.set_indoor_distance_feet(feet)
